/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * HipNetPlugin.cpp
 * A HipNet Controller plugin for ola
 * Copyright (C) 2012 Hippy
 * Copyright (C) 2005-2009 Simon Newton
 *
 * This implementation of HipNet is done purely with posix shared memory segments
 * to spare the networks of the world from this nasty, vile protocol. 
 *
 */

#include <string>
#include "olad/Preferences.h"
#include "plugins/hipnet/HipNetPlugin.h"
#include "plugins/hipnet/HipNetDevice.h"

/*
 * Entry point to this plugin
 */
extern "C" ola::AbstractPlugin* create(
    ola::PluginAdaptor *plugin_adaptor) {
  return new ola::plugin::hipnet::HipNetPlugin(plugin_adaptor);
}

namespace ola {
namespace plugin {
namespace hipnet {

const char HipNetPlugin::HIPNET_NODE_NAME[] = "ola-HipNet";
const char HipNetPlugin::PLUGIN_NAME[] = "HipNet";
const char HipNetPlugin::PLUGIN_PREFIX[] = "hipnet";


/*
 * Start the plugin
 * For now we just have one device.
 */
bool HipNetPlugin::StartHook() {
  m_device = new HipNetDevice(this,
                              m_preferences,
                              m_plugin_adaptor);

  if (!m_device)
    return false;

  if (!m_device->Start()) {
    delete m_device;
    return false;
  }
  m_plugin_adaptor->RegisterDevice(m_device);
    

  return true;
}


/*
 * Stop the plugin
 * @return true on success, false on failure
 */
bool HipNetPlugin::StopHook() {
  if (m_device) {
    bool ret = m_plugin_adaptor->UnregisterDevice(m_device);
    m_device->Stop();
    delete m_device;
    return ret;
  }
  return true;
}


/*
 * Return the description for this plugin
 */
string HipNetPlugin::Description() const {
  return

"XLR (HipNet) Plugin\n"
"--------------------------\n"
"This plugin creates a HipNet controller, and several XLR Ports.\n"
"When patched, XLR's are mapped to universes 1:1.  Eg. Patch XLR 0\n"
"output to OLA universe ID 0, and DMX-512 is sent out of XLR 0 via\n"
"a listening HipNet node.\n"
"\n"
"Patching an XLR input to an OLA universe will change the node's XLR to\n"
"input mode, and you may need a 5-pin gender changer on the XLR port\n"
"to receive DMX-512 from a lighting console.  Eg. patch XLR 0 input\n"
"to OLA universe 0, and the XLR 0 on the node is setup to receive DMX-512.\n";
}

/*
 * Set the default preferences. (none yet)
 */
bool HipNetPlugin::SetDefaultPreferences() {
  if (!m_preferences)
    return false;
/*
  bool save = false;
  save |= m_preferences->SetDefaultValue(HipNetDevice::IP_KEY,
                                         StringValidator(true), ""); 
  if (save)
    m_preferences->Save();
*/
  return true;
}
}  // hipnet
}  // plugin
}  // ola
